class Solution
{
public:
    int profitableSchemes(int n, int m, vector<int>& g, vector<int>& p)
    {
        int len = g.size();
        vector<vector<vector<int>>> dp(len + 1, vector<vector<int>>(n + 1, vector<int>(m + 1)));
        const int MOD = 1e9 + 7;
        for (int j = 0; j <= n; j++)
            dp[0][j][0] = 1;
        for (int i = 1; i <= len; i++)
        {
            for (int j = 0; j <= n; j++)
            {
                for (int k = 0; k <= m; k++)
                {
                    dp[i][j][k] = dp[i - 1][j][k];
                    if (j >= g[i - 1])
                        dp[i][j][k] += dp[i - 1][j - g[i - 1]][max(0, k - p[i - 1])];

                    dp[i][j][k] %= MOD;
                }
            }
        }

        return dp[len][n][m] %= MOD;
    }
};

class Solution
{
public:
    int profitableSchemes(int n, int m, vector<int>& g, vector<int>& p)
    {
        int len = g.size();
        vector<vector<int>> dp(n + 1, vector<int>(m + 1));
        const int MOD = 1e9 + 7;
        for (int j = 0; j <= n; j++)
            dp[j][0] = 1;
        for (int i = 1; i <= len; i++)
        {
            for (int j = n; j >= g[i - 1]; j--)
            {
                for (int k = m; k >= 0; k--)
                {
                    dp[j][k] += dp[j - g[i - 1]][max(0, k - p[i - 1])];

                    dp[j][k] %= MOD;
                }
            }
        }

        return dp[n][m] %= MOD;
    }
};